<?php
class ControllerProductInformation extends Controller { 
	private $error = array();

	public function index() {
		$this->load->language('product/information');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/information');
		$this->getList();
	}

	public function insert() {
		$this->load->language('product/information');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/information');
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->model_product_information->addInformation($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('product/information', 'token=' . $this->session->data['token'], 'SSL'));
		}
		$this->getFormText();
		$this->getInsertForm();
	}

	public function update() {
		$this->load->language('product/information');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/information');
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->model_product_information->editInformation($this->request->get['information_id'], $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('product/information', 'token=' . $this->session->data['token'], 'SSL'));
		}
		$this->getFormText();
		$this->getUpdateForm();
	}

	private function getList() {
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_info_type'] = $this->language->get('column_info_type');
		$this->data['column_title'] = $this->language->get('column_title');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_action'] = $this->language->get('column_action');		
		
		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_delete'] = $this->language->get('button_delete');
 		
		$this->data['token'] = $this->session->data['token'];
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('product/information', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		$this->data['insert'] = $this->url->link('product/information/insert', 'token=' . $this->session->data['token'], 'SSL');
		
		$url = '';
		if (isset($this->request->get['filter_info_type'])) {
			$filter_info_type = $this->request->get['filter_info_type'];
			$url .= '&filter_info_type=' . $this->request->get['filter_info_type'];
		} else {
			$filter_info_type = '';
		}
		if (isset($this->request->get['filter_title'])) {
			$filter_title = $this->request->get['filter_title'];
			$url .= '&filter_title=' . $this->request->get['filter_title'];
		} else {
			$filter_title = '';
		}
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'id.title';
		}
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$this->data['informations'] = array();
		$data = array(
			'filter_info_type'  => $filter_info_type,
			'filter_title'  => $filter_title,
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		$information_total = $this->model_product_information->getTotalInformations($data);
		$results = $this->model_product_information->getInformations($data);
    	foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('product/information/update', 'token=' . $this->session->data['token'] . '&information_id=' . $result['information_id'], 'SSL')
			);
			$this->data['informations'][] = array(
				'information_id' => $result['information_id'],
				'info_type'      => $result['info_type'],
				'status'         => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
				'title'          => $result['title'],
				'action'         => $action
			);
		}	

		$url_sort = $url;
		if ($order == 'ASC') {
			$url_sort .= '&order=DESC';
		} else {
			$url_sort .= '&order=ASC';
		}
		$this->data['sort_info_type'] = $this->url->link('product/information', 'token=' . $this->session->data['token'] . '&sort=i.info_type' . $url_sort, 'SSL');
		$this->data['sort_title'] = $this->url->link('product/information', 'token=' . $this->session->data['token'] . '&sort=id.title' . $url_sort, 'SSL');
		
		$url = '';
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		$pagination = new Pagination();
		$pagination->total = $information_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('product/information', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
		$this->data['pagination'] = $pagination->render();

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'product/information_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}

	private function getFormText() {
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_default'] = $this->language->get('text_default');
		$this->data['text_enabled'] = $this->language->get('text_enabled');
    	$this->data['text_disabled'] = $this->language->get('text_disabled');
		
		$this->data['entry_title'] = $this->language->get('entry_title');
		$this->data['entry_description'] = $this->language->get('entry_description');
		$this->data['entry_info_type'] = $this->language->get('entry_info_type');
		$this->data['entry_layout'] = $this->language->get('entry_layout');
		$this->data['entry_status'] = $this->language->get('entry_status');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
    	
		$this->data['tab_general'] = $this->language->get('tab_general');
    	$this->data['tab_data'] = $this->language->get('tab_data');
		$this->data['tab_design'] = $this->language->get('tab_design');

		$this->data['token'] = $this->session->data['token'];
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),     		
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('product/information', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
		$this->load->model('design/layout');
		$this->data['layouts'] = $this->model_design_layout->getLayouts();
	}

	private function getInsertForm() {
		$this->data['action'] = $this->url->link('product/information/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('product/information', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['information_description'] = array();
		$this->data['info_type'] = '';
		$this->data['layout_id'] = 0;
		$this->data['status'] = 1;
		
		$this->template = 'product/information_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}

	private function getUpdateForm() {
		$this->data['action'] = $this->url->link('product/information/update', 'token=' . $this->session->data['token'] . '&information_id=' . $this->request->get['information_id'], 'SSL');
		$this->data['cancel'] = $this->url->link('product/information', 'token=' . $this->session->data['token'], 'SSL');

		$information_info = $this->model_product_information->getInformation($this->request->get['information_id']);
		$this->data['information_description'] = $this->model_product_information->getInformationDescriptions($this->request->get['information_id']);
		$this->data['info_type'] = $information_info['info_type'];
		$this->data['layout_id'] = $information_info['layout_id'];
		$this->data['status'] = $information_info['status'];
				
		$this->template = 'product/information_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
}
?>